package com.tedu._08aop.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
@Aspect

public class LogAspect {
    //@Before("execution(public int com.tedu._08aop.demo.CalculatorImpl.add(..))")
//    @Before("execution(public * com.tedu._08aop.demo.CalculatorImpl.*(..))")
//    public void before(JoinPoint point){
//        System.out.println("切面类在方法调用前动态添加逻辑了！");
//        Object target = point.getTarget();//获取当前切入点的目标对象
//        System.out.println("target:"+target);
//        //获取类对象
//        Class cls = target.getClass();
////        String packName=cls.getPackage().getName();//获取包名
////        String className=cls.getSimpleName();//获取类名
////        System.out.println("packName:"+className);
//        String className=cls.getName();//完全限定名（包名+类名）
//        System.out.println(className);
//        Signature signature = point.getSignature();//通过切入点获取方法签名
//        String name = signature.getName();
//        System.out.println("方法名:"+name);
//        Object[] args = point.getArgs();
//        System.out.println("方法的所有参数:"+ Arrays.toString(args));
//    }
//    @AfterReturning(
//            value="execution(public * com.tedu._08aop.demo.CalculatorImpl.*(..))",
//            returning = "returnValue")
//    public void afterReturning(JoinPoint point,Object returnValue){
//        Object target = point.getTarget();
//        Class cls = target.getClass();
//        String className=cls.getSimpleName();
//        String methodName=point.getSignature().getName();
//        System.out.println(className+"."+methodName+"()方法调用完毕");
//        System.out.println("返回参数"+returnValue);
//    }
//    @AfterThrowing(value = "execution(public * com.tedu._08aop.demo.CalculatorImpl.*(..))",
//    throwing = "e")
//    public void afterThrowing(JoinPoint point,Exception e){
//        Object target = point.getTarget();
//        Class cls = target.getClass();
//        String className=cls.getSimpleName();
//        String methodName=point.getSignature().getName();
//        System.out.println("切面After Throwing:"+className+"."+methodName+"()抛出异常了"+e.getMessage());
//    }
//    @After("execution(public * com.tedu._08aop.demo.CalculatorImpl.*(..))")
//    public void after(JoinPoint point){
//        Object target = point.getTarget();
//        Class cls = target.getClass();
//        String className = cls.getSimpleName();
//        String methodName = point.getSignature().getName();
//        System.out.println("切面After:"+className+"."+methodName+"()执行完毕");
//    }
    @Around("execution(public * com.tedu._08aop.demo.CalculatorImpl.*(..))")
    public Object around(ProceedingJoinPoint point) {
        String className = point.getTarget().getClass().getSimpleName();
        String methodName=point.getSignature().getName();
        try{
            System.out.println("切面(Around)Before:"+className+"."+methodName+"()调用了");
            Object proceed = point.proceed();//通过切入点对象调用实际方法
            System.out.println("切面(Around)AfterReturning:"+className+"."+methodName+"()调用结束");
            return proceed;
        }catch(Throwable e){
            System.out.println("切面(Around)AfterThrowing:"+className+"."+methodName+"()抛出异常了"+e.getMessage());
            throw new RuntimeException(e);
        }finally {
            //After操作
            System.out.println("切面(Around)After:"+className+"."+methodName+"()调用结束");
        }
    }
}
